package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.common.UserHolder;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //查询分类套餐
    @GetMapping("/list")
    public R<List<ShoppingCart>> select(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,UserHolder.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }

    //添加购物车
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //1.获取用户id
        Long userId = UserHolder.getCurrentId();
        //2.判断是添加套餐还是菜品
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        if (dishId != null) {//菜品
            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {//套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //第一次添加，是新增，后面添加需要修改
        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one == null){//新增
            //set数量和用户id
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }else {//增加数量
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
    }

    //取消购物车
    @PostMapping("/sub")
    public R<ShoppingCart> cancel(@RequestBody ShoppingCart shoppingCart){
        //判断用户取消哪种
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,UserHolder.getCurrentId());
        if (dishId == null){//套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else {//菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }
        //获取数量，判断数量<0，删除套餐
        ShoppingCart cart = shoppingCartService.getOne(qw);
        Integer number = cart.getNumber();
        cart.setNumber(number-1);
        if (number==0){//删除套餐
            shoppingCartService.removeById(cart.getId());
        }else {//减数量
            shoppingCartService.updateById(cart);
        }
        return R.success(cart);
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> delete(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.in(ShoppingCart::getUserId,UserHolder.getCurrentId());
        shoppingCartService.remove(qw);
        return R.success("删除成功");
    }
}
